package com.cxh.rmsq.utils.dialog

import android.content.Context
import android.graphics.Color
import android.widget.TextView
import androidx.core.widget.NestedScrollView
import com.blankj.utilcode.util.TimeUtils
import com.cxh.rmsq.R
import com.hjq.shape.view.ShapeTextView
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.util.XPopupUtils
import kotlin.math.roundToInt


class PlayActivityPopup(context: Context, time: Long, time2: Long, click: onClick) :
    CenterPopupView(context) {

    interface onClick {
        fun click(s: String, s1: String)
    }

    var func: onClick
    var time: Long
    var time2: Long
    var scrollView: NestedScrollView? = null

    init {
        this.func = click
        this.time = time
        this.time2 = time2
    }

    override fun getImplLayoutId(): Int {
        return R.layout.play_popup
    }

    override fun onCreate() {
        super.onCreate()

        var tv_time = findViewById<TextView>(R.id.tv_time)
        scrollView = findViewById<NestedScrollView>(R.id.scrollView)
        var tv_ok = findViewById<ShapeTextView>(R.id.tv_ok)

        tv_time.text = "活动时间：" + TimeUtils.millis2String(time, "yyyy-MM-dd") + "~" + TimeUtils.millis2String(time2, "yyyy-MM-dd")
        scrollView!!.viewTreeObserver.addOnScrollChangedListener {
            if (isScrollViewAtBottom()) {
                tv_ok.isEnabled = true
                tv_ok.setTextColor(resources.getColor(R.color.white))
                tv_ok.getShapeDrawableBuilder()
                    .setSolidColor(resources.getColor(R.color.text_blue))
                    .setStrokeSize(0)
                    .intoBackground()
            } else {
                tv_ok.isEnabled = false
                tv_ok.setTextColor(resources.getColor(R.color.color_333333))
                tv_ok.getShapeDrawableBuilder()
                    .setSolidColor(resources.getColor(R.color.color_e2e2e2))
                    .setStrokeSize(0)
                    .intoBackground()
            }
        }
        tv_ok.setOnClickListener {
            func.click(
                "",
                ""
            )
            dismiss()
        }
    }

    private fun isScrollViewAtBottom(): Boolean {
        return scrollView!!.getScrollY() >= scrollView!!.getChildAt(0)
            .getHeight() - scrollView!!.getHeight()
    }

    // 最大高度为Window的0.85
    override fun getMaxHeight(): Int {
        return ((XPopupUtils.getAppHeight(context) * .85f).roundToInt())
    }

}